import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 徒有琴
 */
public class PipelineTest {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://www.hf1901.com:3306/1901", "root", "root");
        String sql = "SELECT id,name,age FROM person LIMIT ?,?";
        PreparedStatement ps = connection.prepareStatement(sql);
        Jedis jedis = new Jedis("192.168.13.85", 6380);
        Pipeline pipeline = jedis.pipelined();
        long start=System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            ps.setObject(1, i * 100);
            ps.setObject(2, 100);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                String key = "person" + rs.getString("id");
                pipeline.hset(key, "id", rs.getString("id"));
                pipeline.hset(key, "name", rs.getString("name"));
                pipeline.hset(key, "age", rs.getString("age"));
            }
            rs.close();
        }
        pipeline.sync();
        long end=System.currentTimeMillis();
        System.out.println(end-start);
        ps.close();
        connection.close();
        jedis.close();
//        Jedis jedis = new Jedis("192.168.13.85", 6380);
//        long start = System.currentTimeMillis();
//        Pipeline pipeline = jedis.pipelined();
//        for (int i = 0; i < 10000; i++) {
//            //jedis.set(i + "", i + "");
//            pipeline.set(i + "", i + "");
//        }
//        pipeline.sync();
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);//4118    84
//        jedis.close();
    }
}
